package com.skzh.web.controller.media;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.skzh.common.config.SkzhConfig;
import com.skzh.common.core.domain.model.LoginUser;
import com.skzh.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.skzh.common.annotation.Log;
import io.swagger.annotations.ApiOperation;
import com.skzh.common.core.controller.BaseController;
import com.skzh.common.core.domain.AjaxResult;
import com.skzh.common.enums.BusinessType;
import com.skzh.media.domain.TaskMedia;
import com.skzh.media.service.ITaskMediaService;
import com.skzh.common.utils.poi.ExcelUtil;
import com.skzh.common.core.page.TableDataInfo;

/**
 * 任务媒体文件Controller
 *
 * @author skzh
 * @date 2025-04-10
 */
@RestController
@RequestMapping("/media/media")
public class TaskMediaController extends BaseController {
    @Autowired
    private ITaskMediaService taskMediaService;

    /**
     * 查询任务媒体文件列表
     */
    @ApiOperation(value = "查询任务媒体文件列表")
    @PreAuthorize("@ss.hasPermi('media:media:list')")
    @GetMapping("/list" )
    public TableDataInfo list(TaskMedia taskMedia) {
        startPage();
        List<TaskMedia> list = taskMediaService.selectTaskMediaList(taskMedia);
        return getDataTable(list);
    }

    /**
     * 导出任务媒体文件列表
     */
    @ApiOperation(value = "导出任务媒体文件列表")
    @PreAuthorize("@ss.hasPermi('media:media:export')" )
    @Log(title = "任务媒体文件" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, TaskMedia taskMedia) {
        List<TaskMedia> list = taskMediaService.selectTaskMediaList(taskMedia);
        ExcelUtil<TaskMedia> util = new ExcelUtil<TaskMedia>(TaskMedia. class);
        util.exportExcel(response, list, "任务媒体文件数据" );
    }

    /**
     * 根据taskId获取任务媒体文件详细信息
     */
    @ApiOperation(value = "获取任务媒体文件详细信息")
    @PreAuthorize("@ss.hasPermi('media:media:query')" )
    @GetMapping(value = "/{taskId}" )
    public TableDataInfo getInfo(@PathVariable("taskId" ) Long taskId) {
        TaskMedia taskMedia = new TaskMedia();
        taskMedia.setTaskId(taskId);
        List<TaskMedia> list = taskMediaService.selectTaskMediaList(taskMedia);

        //转换文件路径
        String filePath = SkzhConfig.getMediaPath();
        String fileUrl = "http://localhost:8280/profile/media";
        for (TaskMedia tm : list) {
            if (tm.getTmFilePath() != null && tm.getTmFilePath().contains(filePath)) {
                String tmFilePath = tm.getTmFilePath();
                tmFilePath = tmFilePath.replace(filePath, fileUrl);
                tm.setTmFilePath(tmFilePath);
            }
        }

        return getDataTable(list);
    }

    /**
     * 新增任务媒体文件
     */
    @ApiOperation(value = "新增任务媒体文件")
    @PreAuthorize("@ss.hasPermi('media:media:add')" )
    @Log(title = "任务媒体文件" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskMedia taskMedia) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        taskMedia.setCreateBy(loginUser.getUsername());
        return toAjax(taskMediaService.insertTaskMedia(taskMedia));
    }

    /**
     * 修改任务媒体文件
     */
    @ApiOperation(value = "修改任务媒体文件")
    @PreAuthorize("@ss.hasPermi('media:media:edit')" )
    @Log(title = "任务媒体文件" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TaskMedia taskMedia) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        taskMedia.setUpdateBy(loginUser.getUsername());
        return toAjax(taskMediaService.updateTaskMedia(taskMedia));
    }

    /**
     * 删除任务媒体文件
     */
    @ApiOperation(value = "删除任务媒体文件")
    @PreAuthorize("@ss.hasPermi('media:media:remove')" )
    @Log(title = "任务媒体文件" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{tmIds}" )
    public AjaxResult remove(@PathVariable Long[] tmIds) {
        return toAjax(taskMediaService.deleteTaskMediaByTmIds(tmIds));
    }

    /**
     * 清除所有视频记录
     */
    @ApiOperation(value = "删除任务执行记录")
    @PreAuthorize("@ss.hasPermi('history:history:remove')" )
    @Log(title = "任务执行记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/cleanMediaVideoByTaskId/{taskIds}" )
    public AjaxResult cleanMediaVideoByTaskId(@PathVariable Long[] taskIds) {
        return toAjax(taskMediaService.deleteMediaVideoByTaskId(taskIds));
    }
}
